package com.aba.hos.totality;


import com.aba.hos.admin.Admin;
import com.aba.hos.admin.AdminLoginExampleWindow;
import com.aba.hos.doc.gui.HospitalManagementGUI;
import com.aba.hos.pat.patientExampleWindow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Map;

public class LoginWindow extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new LoginWindow();
        });
    }

    public LoginWindow() {
        // 设置标题
        setTitle("请登陆");
        // 绝对布局
        setLayout(null);

        // 定义一个容器
        Container c = getContentPane();

        // 创建"账号:"标签
        JLabel accountLabel = new JLabel("账号:");
        // 创建账号文本框
        final JTextField accountField = new JTextField();
        // 创建"密码:"标签
        JLabel passwordLabel = new JLabel("密码:");
        // 创建密码框
        final JPasswordField passwordField = new JPasswordField();
        // 设置密码字符为*
        passwordField.setEchoChar('*');
        // 创建"登录"按钮
        JButton loginButton = new JButton("登录");
        // 创建"重置"按钮
        JButton resetButton = new JButton("重置");

        // 登录按钮点击事件处理
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取账号和密码输入值
                String account = accountField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();

                GetRole getRole =new GetRole();
                // 校验是否输入
                if (account.isEmpty() || password.isEmpty()) {
                    showCentralizedMessageDialog("不能为空!");
                }
                //登录请求
                Map<Integer, Admin> role = null;
                try {
                    role = getRole.getRole(account,password);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }

                switch(role.keySet().iterator().next()) {
                    //管理员
                    case 1:showCentralizedMessageDialog("登录成功,欢迎您 "+role.get(1).getRole()+" : "+role.get(1).getName());
                        // 关闭当前登录窗口
                        dispose();
                        // 打开管理员主页面
                        Map<Integer, Admin> finalRole = role;
                        SwingUtilities.invokeLater(() -> {
                            //调用admin的代码
                            AdminLoginExampleWindow adminLoginExample = new AdminLoginExampleWindow();
                            //调用admin的窗口代码
                            adminLoginExample.openMainPage(account, "管理员");
                        });
                        break;
                    //医生
                    case 2:showCentralizedMessageDialog("登录成功,欢迎您管理员"+" : "+role.get(2).getName());
                        // 关闭当前登录窗口
                        dispose();
                        // 打开医生主页面
                        Map<Integer, Admin> finalRole2 = role;
                        SwingUtilities.invokeLater(() -> {
                            HospitalManagementGUI gui = null;
                            Map<Integer, Admin> finalRoleDoc = finalRole2;
                            try {
                                gui = new HospitalManagementGUI(finalRoleDoc.get(2).getName(),"医生");
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }
                            gui.setVisible(true);
                        });
                        break;
                    //患者
                    case 3:showCentralizedMessageDialog("登录成功,欢迎您 "+role.get(3).getRole()+" : "+role.get(3).getName());
                        // 关闭当前登录窗口
                        dispose();
                        // 打开病患主页面
                        Map<Integer, Admin> finalRole1 = role;
                        SwingUtilities.invokeLater(() -> {
                            try {
                                new patientExampleWindow().openMainPage(finalRole1.get(3).getName(), "病人");
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }
                        });
                        break;

                    default:showCentralizedMessageDialog("账号密码错误");
                }
            }
        });

        // 重置按钮点击事件处理
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 清空账号和密码输入框
                accountField.setText("");
                passwordField.setText("");
            }
        });

        // 将各组件添加到容器中
        c.add(accountLabel);
        c.add(accountField);
        c.add(passwordLabel);
        c.add(passwordField);
        c.add(loginButton);
        c.add(resetButton);

        // 设置各组件的位置以及大小
        accountLabel.setBounds(25, 20, 90, 30);
        accountField.setBounds(60, 20, 210, 30);
        passwordLabel.setBounds(25, 60, 90, 30);
        passwordField.setBounds(60, 60, 210, 30);
        loginButton.setBounds(80, 100, 70, 50);
        resetButton.setBounds(150, 100, 70, 50);

        // 设置窗体大小、关闭方式、不可拉伸
        setSize(310, 225);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 设置窗口居中显示
        setLocationRelativeTo(null);
        setVisible(true);
    }

    // 显示居中的消息对话框
    private void showCentralizedMessageDialog(String message) {
        JFrame tempFrame = new JFrame();
        tempFrame.setAlwaysOnTop(true);
        tempFrame.setUndecorated(true);
        tempFrame.setOpacity(0.0f);
        tempFrame.setSize(0, 0);
        tempFrame.setLocationRelativeTo(null); // 使临时窗口居中
        tempFrame.setVisible(true);

        JOptionPane.showMessageDialog(tempFrame, message);
        tempFrame.dispose();
    }
}